package com.csc531.adapters;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.csc531.DataTypes.Note;
import com.csc531.views.NotesView;

/**
 * @author Javier Figueroa
 * Adapter for the NotesThinList class
 *
 */
public class NotesSpeechListAdapter extends BaseAdapter {

	//Logging
	private static final String TAG = "notes list adapter";
	
	protected Context mContext;
	protected int _position = 0;
	protected ArrayList<Boolean> _mExpanded;
	private ArrayList<Note> _notes;


	/**
	 * @param context
	 */
	public NotesSpeechListAdapter(Context context) {
		mContext = context;
		_notes = new ArrayList<Note>();
		_mExpanded = new ArrayList<Boolean>();

	}

	/**
	 * The number of items in the list is determined by the number of speeches
	 * in our ArrayList.
	 * 
	 * @see android.widget.ListAdapter#getCount()
	 */
	public int getCount() {
		return _notes.size();
	}

	/**
	 * Return object in the ArrayList
	 * 
	 * @see android.widget.ListAdapter#getItem(int)
	 */
	public Object getItem(int position) {
		return _notes.get(position);
	}

	/**
	 * Use the ArrayList index as a unique id.
	 * @see android.widget.ListAdapter#getItemId(int)
	 */
	public long getItemId(int position) {
		return position;
	}


	/**
	 * Appends notes to the list adapter
	 * 
	 * @param newNote note to be added to the adapter
	 */
	public void addNote(Note newNote) {
		_notes.add(newNote);
	}

	/**
	 * Populates list with contents of the list adapter
	 */
	public void createGlobalTaskList() {
		int sizeOfList = _notes.size();
		int i = 0;
		while( i < sizeOfList ) {
			_mExpanded.add(false);
			i++;
		}
	}


	/* (non-Javadoc)
	 * @see com.csc531.adapters.SpeechListAdapter#getView(int, android.view.View, android.view.ViewGroup)
	 */
	public View getView(int position, View convertView, ViewGroup parent) {
		NotesView sv;

		Note note = (Note) _notes.get(position);
		if (convertView == null) {
			sv = new NotesView(mContext, note, _mExpanded.get(position));
		} else {
			sv = (NotesView)convertView;
			sv.setTitle(note);
			sv.setDialogue(note);
			sv.setExpanded(_mExpanded.get(position));
		}

		return sv;
	}

	/**
	 * Toggles visibility of the description field of the task
	 * 
	 * @param position
	 */
	public void toggle(int position) {
		boolean pos = _mExpanded.get(position);
		Log.v(TAG,"pos is: 	"+pos);

		_mExpanded.set(position,!_mExpanded.get(position));
		notifyDataSetChanged();
	}

}
